package cn.yunyichina.provider.bed.service.impl;

import cn.yunyichina.provider.bed.dao.MedicalCardDao;
import cn.yunyichina.provider.bed.entity.MedicalCard;
import cn.yunyichina.provider.bed.vo.BedVo;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.base.PatientVo;
import cn.yunyichina.provider.iface.entity.hisiface.base.PatientVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/13 0013.
 * 住院号管理
 */
@Service("admissionnoServiceImpl")
public class AdmissionNoServiceImpl {

    private Logger logger = LoggerFactory.getLogger(AdmissionNoServiceImpl.class);

    @Autowired
    private MedicalCardDao medicalCardDao;

    /**
     * 出入院-获取住院号
     * @param data
     * @param sessionId
     * @return
     */
    public ResponseObject getAdmissionno(String data, String sessionId){
        //定义返回结果
        ResponseObject response = new ResponseObject();
        Map<String, Object> result = new HashMap<>();
        String admissionNo = "";

        //定义返回码和返回信息
        String resultCode = "0";
        String resultMessage = "执行成功";

        try {
            //获取入参
            logger.info("【出入院-获取住院号】开始, 请求="+data,",sessionId=" + sessionId);
            BedVo bedVo = JsonUtils.parseObject(data, BedVo.class);

            //检查参数合法性
            boolean check = true;
            if(StringUtils.isBlank(bedVo.getMedicalCardId())){
                check = false;
                resultCode = "14002";
                resultMessage = "参数有误:" + "medical_card_id绑定卡id不能为空";
            }

            //优先通过就诊卡表查询住院号
            MedicalCard medicalCard = new MedicalCard();
            medicalCard = medicalCardDao.selectById(bedVo.getMedicalCardId());

            if(medicalCard != null){
                if(StringUtils.isNotBlank(medicalCard.getAdmissionNo())){
                    admissionNo = medicalCard.getAdmissionNo();
                }else{
                    admissionNo = "";
                }
            }else{
                check = false;
                resultCode = "14003";
                resultMessage = "无此绑定卡信息";
                admissionNo = "";
            }

            //检查不通过，结束流程
            if(!check){
                return response;
            }

            //就诊卡表查询住院号无记录则通过诊疗卡查询患者信息接口获取住院号
            if(StringUtils.isBlank(admissionNo)){
                PatientVo patientVo = new PatientVo();
                patientVo.setPatName(medicalCard.getPatName());
                patientVo.setPatCardNo(medicalCard.getMedicalCardNo());
                patientVo.setPatCardType(medicalCard.getMedicalCardType().toString());
                ResCommon<PatientVoResponse> patientVoResponse = HisifaceService.getHisifaceClient().getPatient(patientVo);

                //如果访问his失败
                if (!"0".equals(patientVoResponse.getResultCode())){
                    response.setResultCodeAndMessage("14998",patientVoResponse.getResultMessage());
                    admissionNo = "";
                }else{
                    PatientVoResponse patient = patientVoResponse.getResult();
                    admissionNo = patient.getAdmissionNo();
                }
            }
        } catch (Exception e) {
            resultCode = "14999";
            resultMessage = "系统异常:" + e.getMessage();
            e.printStackTrace();
        } finally {
            //组装返回结果
            result.put("admission_no",admissionNo);
            response.setResultCodeAndMessageAndResult(resultCode, resultMessage, JsonUtils.toJsonString(result));
            logger.info("【出入院-获取住院号】结束, 返回结果=" + JsonUtils.toJsonString(response));
            return response;
        }
    }

    /**
     * 出入院-保存住院号
     * @param data
     * @param sessionId
     * @return
     */
    public ResponseObject saveAdmissionno(String data, String sessionId){
        //定义返回结果
        ResponseObject response = new ResponseObject();
        Map<String, Object> result = new HashMap<>();

        //定义返回码和返回信息
        String resultCode = "0";
        String resultMessage = "执行成功";

        try {
            //获取入参
            logger.info("【出入院-保存住院号】开始, 请求="+data,",sessionId=" + sessionId);
            BedVo bedVo = JsonUtils.parseObject(data, BedVo.class);

            //检查参数合法性
            boolean check = true;
            if(StringUtils.isBlank(bedVo.getAdmissionNo())){
                check = false;
                resultCode = "14002";
                resultMessage = "参数有误:" + "admission_no住院号不能为空";
            }

            if(StringUtils.isBlank(bedVo.getMedicalCardId())){
                check = false;
                resultCode = "14002";
                resultMessage = "参数有误:" + "medical_card_id绑定卡id不能为空";
            }

            MedicalCard medicalCard = new MedicalCard();
            medicalCard = medicalCardDao.selectById(bedVo.getMedicalCardId());

            if(medicalCard != null){
                medicalCard.setAdmissionNo(bedVo.getAdmissionNo());
                medicalCardDao.update(medicalCard);
            }else{
                check = false;
                resultCode = "14003";
                resultMessage = "无此绑定卡信息";
            }

            //检查不通过，结束流程
            if(!check){
                return response;
            }
        } catch (Exception e) {
            resultCode = "14999";
            resultMessage = "系统异常:" + e.getMessage();
            e.printStackTrace();
        } finally {
            //组装返回结果
            response.setResultCodeAndMessageAndResult(resultCode, resultMessage, JsonUtils.toJsonString(result));
            logger.info("【出入院-保存住院号】结束, 返回结果=" + JsonUtils.toJsonString(response));
            return response;
        }
    }
}
